package com.mljqqh.controller;

import com.leyou.common.pojo.PageResult;
import com.leyou.item.pojo.Brand;
import com.mljqqh.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*

 */
@RequestMapping("brand")
@RestController //指定是controller层
public class BrandController {
    @Autowired
    BrandService brandService ;

    @GetMapping("page")
    private ResponseEntity<PageResult<Brand>> queryBrandByPage(
            @RequestParam(value = "key", required = false)String key,
            @RequestParam(value = "page", defaultValue = "1")Integer page,
            @RequestParam(value = "rows", defaultValue = "5")Integer rows,
            @RequestParam(value = "sortBy", required = false)String sortBy,
            @RequestParam(value = "desc",required = false)Boolean desc
    ){
       PageResult<Brand> result = this.brandService.queryBrandsByPage(key,page,rows,sortBy,desc);
        //404
        if (CollectionUtils.isEmpty(result.getItems())){
            return ResponseEntity.notFound().build();
        }

        return ResponseEntity.ok(result);



    }

    @PostMapping
    public ResponseEntity<Void> saveBrand(Brand brand ,@RequestParam("cids") List<Long> cids){

        this.brandService.saveBrand(brand,cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();



    }


}

